package com.data.modules.online.cgform.enhance.impl.http.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.data.common.util.RestUtil;
import com.data.common.util.SpringContextUtils;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.cgform.entity.OnlCgformEnhanceJava;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

public interface CgformEnhanceHttpInter {
    public static final Logger logger = LoggerFactory.getLogger(CgformEnhanceHttpInter.class);

    default int execute(String tableName, JSONObject json, OnlCgformEnhanceJava enhance) {
        return 1;
    }

    default void execute(String tableName, List<Map<String, Object>> list, OnlCgformEnhanceJava enhance) {
    }

    default Object sendPost(JSONObject params, OnlCgformEnhanceJava enhance) {
        if (enhance == null) {
            return null;
        }
        String cgJavaValue = enhance.getCgJavaValue();
        if (ConvertUtils.isEmpty(cgJavaValue)) {
            return null;
        }
        if (!cgJavaValue.startsWith("http") && !cgJavaValue.startsWith("https")) {
            String baseUrl = RestUtil.getBaseUrl();
            cgJavaValue = cgJavaValue.startsWith("/") ? baseUrl + cgJavaValue : baseUrl + "/" + cgJavaValue;
        }
        ResponseEntity request = RestUtil.request(cgJavaValue, HttpMethod.POST, getHeaders(SpringContextUtils.getHttpServletRequest()), (JSONObject) null, params, String.class);
        if (request.getStatusCode() != HttpStatus.OK) {
            return null;
        }
        String str = (String) request.getBody();
        if (!ConvertUtils.isNotEmpty(str)) {
            return null;
        }
        try {
            JSONObject parseObject = JSON.parseObject(str);
            if (parseObject.getBoolean("success").booleanValue()) {
                return parseObject.get("result");
            }
            return null;
        } catch (Exception e) {
            logger.warn("请求Online表单Java增强http接口时转换数据出错：" + e.getMessage() + "\n body: " + str);
            return null;
        }
    }

    default HttpHeaders getHeaders(HttpServletRequest request) {
        HttpHeaders httpHeaders = new HttpHeaders();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String str = (String) headerNames.nextElement();
            Enumeration headers = request.getHeaders(str);
            while (headers.hasMoreElements()) {
                httpHeaders.set(str, (String) headers.nextElement());
            }
        }
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.remove("Accept-Encoding");
        httpHeaders.remove("accept-encoding");
        httpHeaders.remove("Accept");
        httpHeaders.add("Accept", "application/json");
        return httpHeaders;
    }
}
